package com.platform.server.util;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.MessageDigest;

import org.apache.commons.codec.binary.Base64;

/**
 * <p>
 * 安全工具类
 * </p>
 * 
 */
public class SecurityUtil {

	/**
	 * 校验签名
	 * 
	 * @param sign
	 *            签名
	 * @param salt
	 *            指定签名的盐
	 * @param params
	 *            用于签名的参数列表
	 * @return
	 */
	public static boolean validateSign(String sign, String salt, Object... params) {
		if (null == sign) {
			return false;
		}
		String valSign = getSign(salt, params);
		return !(null == valSign || !sign.equalsIgnoreCase(valSign));
	}


	/**
	 * 签名
	 * 
	 * @param salt
	 *            加盐
	 * @param params
	 *            加密参数
	 * @return
	 */
	public static String getSign(String salt, Object... params) {
		StringBuffer buf = new StringBuffer();
		if (null != params) {
			for (Object s : params) {
				buf.append(null == s ? "" : s);
			}
		}
		buf.append(null == salt ? "" : salt);
		return getInnerSign(buf.toString());
	}

	/**
	 * 签名
	 * 
	 * @param str
	 * @return
	 */
	private static String getInnerSign(String str) {
		if (null == str) {
			return "";
		}
		return getMD5(str);
	}

	/**
	 * 计算MD5
	 * 
	 * @param str
	 * @return
	 */
	public static String getMD5(String str) {
		if (null == str) {
			return "";
		}
		MessageDigest messageDigest = null;
		try {
			messageDigest = MessageDigest.getInstance("MD5");
			messageDigest.reset();
			messageDigest.update(str.getBytes("UTF-8"));
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		byte[] byteArray = messageDigest.digest();
		StringBuffer md5StrBuff = new StringBuffer();
		for (int i = 0; i < byteArray.length; i++) {
			if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
				md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
			else
				md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
		}
		return md5StrBuff.toString();
	}

	/**
	 * 将字符串进行Base64解码
	 * 
	 * @param str
	 * @return
	 */
	public static String decodeBase64(String str) {
		return decodeBase64(str, "UTF-8");
	}

	/**
	 * 将字符串进行Base64解码
	 * 
	 * @param str
	 * @param charset
	 *            字符集
	 * @return
	 */
	public static String decodeBase64(String str, String charset) {
		if (str == null) {
			return null;
		}
		try {
			return new String(Base64.decodeBase64(str.getBytes()), charset);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 将字符串进行base64转码
	 * 
	 * @param str
	 * @return
	 */
	public static String encodeBase64(String str) {
		return encodeBase64(str, "UTF-8");
	}

	/**
	 * 将字符串进行base64转码
	 * 
	 * @param str
	 * @param charset
	 *            字符集
	 * @return
	 */
	public static String encodeBase64(String str, String charset) {
		if (str == null) {
			return null;
		}
		try {
			return new String(Base64.encodeBase64(str.getBytes()), charset);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 直接对字节数组进行base64转码
	 * 
	 * @param data
	 * @return
	 */
	public static byte[] encodeBase64(byte[] data) {
		try {
			return Base64.encodeBase64(data);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 直接对字节数组进行base64解码
	 * 
	 * @param data
	 * @return
	 */
	public static byte[] decodeBase64(byte[] data) {
		try {
			return Base64.decodeBase64(data);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 获取加密后的密码
	 * 
	 * @param passportName
	 *            账号名称
	 * @param pwd
	 *            密码
	 * @return
	 * @throws Exception
	 */
	public static String getPWD(String passportName, String pwd) {
		StringBuilder sb = new StringBuilder();
		sb.append(passportName.toLowerCase()).append(" ").append(pwd);
		String password = getMD5(sb.toString());
		return password;
	}


	public static String urlEncode(String str) {
		return urlEncode(str, "utf-8");
	}

	public static String urlEncode(String str, String charset) {
		try {
			return URLEncoder.encode(str, charset);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return "";
		}
	}

	public static String urlDecode(String str) {
		return urlDecode(str, "utf-8");
	}

	public static String urlDecode(String str, String charset) {
		try {
			return URLDecoder.decode(str, charset);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return "";
		}
	}
}
